﻿using LxBasic;
using LxCore;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace LxCFD
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            LogWin l = new LogWin(ConsoleWin);
            LxLog.SetLog(l);
            this.DataContext = new MainWindowViewModel();
        }
        public class LogWin : LxLog.ILog
        {
            public LogWin(RichTextBox richtextbox)
            {
                this.richtextbox = richtextbox;
                this.richtextbox.Document.Blocks.Clear();
                richtextbox.FontSize = 12.5;
            }
            double offsetLog = 0;
            int maxLines = 500;
            double LineHeight = 2;
            RichTextBox richtextbox;
            protected void WriteLine(string message, bool addTime, Brush brush)
            {
                richtextbox.Dispatcher.BeginInvoke(() =>
                {
                    if (addTime) { message = string.Format("{0} :  {1}", DateTime.Now, message); }
                    Paragraph p = new Paragraph(new Run(message));
                    p.Foreground = brush;
                    p.LineHeight = LineHeight;

                    richtextbox.Document.Blocks.Add(p);
                    if (richtextbox.Document.Blocks.Count > maxLines + 20)
                    {
                        for (int i = maxLines; i < richtextbox.Document.Blocks.Count; i++)
                        {
                            richtextbox.Document.Blocks.Remove(richtextbox.Document.Blocks.FirstBlock);
                        }
                    }
                    if (richtextbox.VerticalOffset >= offsetLog)
                    {
                        richtextbox.ScrollToEnd();
                        offsetLog = richtextbox.VerticalOffset;
                    }
                });
            }
            public void Calc(string message)
            {
                WriteLine(message, false, Brushes.Black);
            }
            public void Info(string message)
            {
                WriteLine(message, true, Brushes.Black);
            }
            public void Warn(string message)
            {
                WriteLine(message, true, Brushes.Orange);
            }
            public void Error(string message)
            {
                WriteLine(message, true, Brushes.Red);
            }

            public void Debug(string message)
            {
                WriteLine(message, true, Brushes.DarkGray);
            }
        }

    }

}
